<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>loki</title>
</head>
<body>
<script src="https://cdn.bootcdn.net/ajax/libs/lokijs/1.5.9/lokijs.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lokijs/1.5.9/loki-indexed-adapter.min.js"></script>
<script type="module">
    console.log('run')

    var adapter = new LokiIndexedAdapter();
    var db = new loki('Example', {
        adapter,
        autoloadCallback : ()=>{
            console.log('autoloadCallback')
        },
        autoload: true,
        autosave: true,
        autosaveInterval: 4000
    });

    const coll = db.getCollection('users')
    console.log('判断是否存在 collection', coll)
    if (!coll) {
        console.log('无 coll, 需要创建 users')
        // create col
        var users = db.addCollection('users', {indices: ['email']});
        // add data
        var odin = users.insert({name: 'odin', email: 'odin.soap@lokijs.org', age: 38});
        var thor = users.insert({name: 'thor', email: 'thor.soap@lokijs.org', age: 25});
        var stan = users.insert({name: 'stan', email: 'stan.soap@lokijs.org', age: 29});
        var oliver = users.insert({name: 'oliver', email: 'oliver.soap@lokijs.org', age: 31});
        var hector = users.insert({name: 'hector', email: 'hector.soap@lokijs.org', age: 15});
        var achilles = users.insert({name: 'achilles', email: 'achilles.soap@lokijs.org', age: 31});
        console.log(users)
    }

    console.log(coll)
</script>
</body>
</html>
